/**
 * 
 */
package touro.mancala.client;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;

import touro.mancala.core.Constants;

/**
 * @author Baila
 *
 */
public class AIPlayer extends Player {

	private static final Logger logger = Logger.getLogger( AIPlayer.class.getName() );
	
	Socket socket;
	
	public AIPlayer(String name) 
	{
		super(name);
		try {
			socket = new Socket("192.168.117.116", Constants.CLIENT_PORT);
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @param board 
	 * 
	 */
	public int getMove(Board board) {
		String boardString = board.toString()+" "+GameState.getGameState().getActivePlayer()+"\n";
		try {
			//board as String activeplayer newLineCharacter.
		
			logger.log(Level.INFO,"Writing " + boardString);
			
			OutputStream out = socket.getOutputStream();
			out.write(boardString.getBytes());
			out.flush();
			
			logger.log(Level.INFO,"Blocking on READ");
			
			InputStream in = socket.getInputStream();
			BufferedReader reader = new BufferedReader(new InputStreamReader(in));
			String s = reader.readLine();
			
			logger.log(Level.INFO,"Read "+s);
			
			return Integer.parseInt(s);
						
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return -1;
		}
	}
}
